//
//  CBJHomeViewController.swift
//  Weibo
//
//  Created by 崔健 on 16/5/19.
//  Copyright © 2016年 cbj. All rights reserved.
//

import UIKit


class CBJHomeViewController: CBJBaseController {

    
    let statuesListVM : CBJStatuesListViewModel = CBJStatuesListViewModel()
    
    private lazy var activityView :UIActivityIndicatorView = {
        
            let activityView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
            return activityView
        }()
    
    private lazy var refreshView : CBJRefreshView = CBJRefreshView(frame: CGRect(x: 0, y: -60, width: kScreenWidth, height: 60))
    
    func  setupUI(){
    
        tableView.addSubview(refreshView)
        tableView.separatorStyle = .None
        refreshView.addTarget(self, action: #selector(loadData), forControlEvents: .ValueChanged)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "首页"
        self.tableView.tableFooterView = activityView
        visitView.updateUI("关注一些人,回到这里看看有什么惊喜", imageName: "visitordiscover_feed_image_smallicon",isHome: true)
        
        setRightItem()
        
        if tableView == nil {
            
            return
        }
        
        setupUI()
        
        tableView.estimatedRowHeight = 300

        tableView.registerClass(CBJStatuesCell.self, forCellReuseIdentifier: statuesCellId)
        
        loadData()
        
//        refreshControl = UIRefreshControl()
//        refreshControl?.addTarget(self, action: #selector(loadData), forControlEvents: .ValueChanged)
        
    }
    
    
    @objc private func loadData(){
    
        statuesListVM.loadStatusData(activityView.isAnimating()) { (isSuccess) in
         
            if self.refreshView.flyType == .flying {
            
                self.tableView.contentInset.top += -60
            }
            
            self.activityView.stopAnimating()
            self.refreshView.flyType = .toFly
            self.refreshView.activityView.stopAnimating()
            self.refreshView.arrowImage.hidden = false
            if isSuccess{
            
                self.refreshControl?.endRefreshing()
                self.tableView.reloadData()
            }
        }
    }
    

    func setRightItem() {
        
        let rightBtn = UIButton(type: .Custom)
        rightBtn.addTarget(self, action: #selector(rightBtnDidClick), forControlEvents: .TouchUpInside)
        rightBtn.setBackgroundImage(UIImage.init(named: "navigationbar_pop"), forState: .Normal)
        rightBtn.setBackgroundImage(UIImage.init(named: "navigationbar_pop_highlighted"), forState: .Highlighted)
        let rightBtnItem = UIBarButtonItem(customView: rightBtn)
        navigationItem.rightBarButtonItem = rightBtnItem
        rightBtn.sizeToFit()
    }
    
    @objc private func rightBtnDidClick(){
    
        
        let vc = CBJHomeDetailController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
}


extension CBJHomeViewController{
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statuesListVM.statuesList.count
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(statuesCellId, forIndexPath: indexPath) as! CBJStatuesCell
        
        cell.statuesVM = statuesListVM.statuesList[indexPath.row]
        
        // 静默加载
        if indexPath.row == self.statuesListVM.statuesList.count - 1 && !activityView.isAnimating(){
    
            activityView.startAnimating()
            loadData()
        }
        
        return cell
    }
    
    
    
}



